package cn.tansanqinger.dao.sql.group;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.ICommentTopicSqlDao;
import cn.tansanqinger.dao.sql.IReportUserSqlDao;
import cn.tansanqinger.pojo.Commenttopic;
import cn.tansanqinger.pojo.Reportuser;

@Repository
public class ReportUserAndGroupSqlDaoImpl {

	@Autowired
	private IReportUserSqlDao dao;
	@Autowired
	private ICommentTopicSqlDao cdao;
	
	/**
	 * 查询举报信息
	 * @param id
	 * @param uandg
	 * @return
	 */
	public List<Reportuser> selectReport(int id, int uandg){
		QueryWrapper<Reportuser> queryWrapper = new QueryWrapper<Reportuser>();
		queryWrapper.lambda().eq(Reportuser::getIid, id).eq(Reportuser::getUserandgroup, uandg).eq(Reportuser::getRisdispose, 0);
		return dao.selectList(queryWrapper);
	}
	
	/**
	 * 查询举报信息
	 * @param starttime
	 * @param endtime
	 * @param page
	 * @param limit
	 * @return
	 */
	public Map<String, Object> selectReportComment(long starttime, long endtime, int page, int limit){
		QueryWrapper<Reportuser> queryWrapper = new QueryWrapper<Reportuser>();
		if(starttime!=-1&&endtime!=-1) {
			queryWrapper.lambda().ge(Reportuser::getRtime,starttime).le(Reportuser::getRtime,endtime);
		}
		queryWrapper.lambda().eq(Reportuser::getRisdispose, 0).eq(Reportuser::getUserandgroup, 2);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("count", dao.selectCount(queryWrapper));
		Page<Reportuser> pg = new Page<Reportuser>(page, limit);
		IPage<Reportuser> iPage = dao.selectPage(pg, queryWrapper);
		List<Reportuser> coursesList = iPage.getRecords();
		for (int i = 0; i < coursesList.size(); i++) {
			Commenttopic commenttopic = cdao.selectById(coursesList.get(i).getIid());
			if(commenttopic!=null) {
				coursesList.get(i).setRdisposemessage(commenttopic.getCmessage());
				coursesList.get(i).setUname(commenttopic.getCdate());
			} else {
				coursesList.get(i).setRisdispose(1);
				dao.updateById(coursesList.get(i));
			}
		}
		resultMap.put("data", coursesList);
		resultMap.put("code","0");
		resultMap.put("msg", "查询成功");
		return resultMap;
	}
	
	//public List<Reportuser> selectReport
	/**
	 * 查询单一信息
	 * @param id
	 * @return
	 */
	public Reportuser selectReportuserOne(int id) {
		return dao.selectById(id);
	}
	
	/**
	 * 插入处理结果
	 * @param reportuser
	 * @return
	 */
	public int modification(Reportuser reportuser) {
		return dao.updateById(reportuser);
	}
	
	/**
	 * 忽略的时候删除信息
	 * @param id
	 * @return
	 */
	public int delete(int id) {
		return dao.deleteById(id);
	}
}
